#!/bin/sh
#
# Set the system hostname.
#

set -e

. /usr/libexec/os-helpers-logging
. /usr/sbin/balena-config-vars

info "Setting hostname."

if [ -z "$CONFIG_HOSTNAME" ]; then
    # take just the first 7 characters
    info "Generating default based on short UUID."
    CONFIG_HOSTNAME=$(echo "$UUID" | sed -e 's/\(.......\).*/\1/')
fi

if [ -z "$CONFIG_HOSTNAME" ]; then
    fail "UUID missing from config.json."
fi

# Set the transient hostname.
hostname "$CONFIG_HOSTNAME"
# Set the static hostname.
echo "$CONFIG_HOSTNAME" > /etc/hostname

info "Hostname set to $CONFIG_HOSTNAME"

exit 0
